package com.pkx.cloud.fse7.expand.group.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.pkx.cloud.fse7.dms.realtimestate.service.RealTimeStateService;
import com.pkx.cloud.fse7.expand.group.entity.DoorGroup;
import com.pkx.cloud.fse7.expand.group.entity.DoorGroupDetail;
import com.pkx.cloud.fse7.expand.group.repo.DoorGroupDetailMapper;
import com.pkx.cloud.fse7.expand.group.service.DoorGroupDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import javax.annotation.Resource;

/**
 * 服务实现类
 *
 * @author chenwg
 * @since 2021-04-09
 */
@Service
@DS("slave")
public class DoorGroupDetailServiceImpl extends ServiceImpl<DoorGroupDetailMapper, DoorGroupDetail> implements DoorGroupDetailService {

    @Resource
    private RealTimeStateService realTimeStateService;

    /**
     * 分页列表
     *
     * @param doorGroupDetail
     * @return
     */
    @Override
    public Page<DoorGroupDetail> pageList(Page page, DoorGroupDetail doorGroupDetail) {
        LambdaQueryWrapper<DoorGroupDetail> lambdaQueryWrapper = Wrappers.lambdaQuery(DoorGroupDetail.class);
        lambdaQueryWrapper.eq(DoorGroupDetail::getGroupId, doorGroupDetail.getGroupId());
        return page(page, lambdaQueryWrapper);
    }

    /**
     * 列表查询
     *
     * @param doorGroupDetail
     * @return
     */
    @Override
    public List<DoorGroupDetail> listByEntity(DoorGroupDetail doorGroupDetail) {
        LambdaQueryWrapper<DoorGroupDetail> lambdaQueryWrapper = Wrappers.lambdaQuery(DoorGroupDetail.class);
        return list(lambdaQueryWrapper);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public DoorGroupDetail selectById(String id) {
        return getById(id);
    }

    /**
     * 保存
     *
     * @param doorGroupDetail
     * @return
     */
    @Override
    public Boolean saveDoorGroupDetail(DoorGroupDetail doorGroupDetail) {
        return save(doorGroupDetail);
    }

    /**
     * 修改
     *
     * @param doorGroupDetail
     * @return
     */
    @Override
    public Boolean modify(DoorGroupDetail doorGroupDetail) {
        return updateById(doorGroupDetail);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public Boolean deleteById(String id) {
        return removeById(id);
    }

    @Override
    public Boolean deleteByGroupId(String groupId) {
        Set<String> collect = lambdaQuery().eq(DoorGroupDetail::getGroupId, groupId)
                .list()
                .stream()
                .map(DoorGroupDetail::getId)
                .collect(Collectors.toSet());
        return removeByIds(collect);
    }

    @Override
    public Page<List<Map<String, Object>>> getGroupDetailByPath(Page page, DoorGroup doorGroup) {
        List<DoorGroupDetail> groupDetailByPath = baseMapper.getGroupDetailByPath(doorGroup);
        if (groupDetailByPath.isEmpty()) {
            return page;
        }
        Set<String> collect = groupDetailByPath.stream().map(DoorGroupDetail::getDevId).collect(Collectors.toSet());
        Page<List<Map<String, Object>>> listPage = realTimeStateService.deviceList(page, collect);
        return listPage;
    }

    @Override
    public List<DoorGroupDetail> getAllByPlanId(String planId) {
        return baseMapper.getAllByPlanId(planId);
    }

    @Override
    public List<DoorGroupDetail> getAllByPlanIds(List<String> planIds) {
        return baseMapper.getAllByPlanIds(planIds);
    }

    @Override
    public List<DoorGroupDetail> getAllByGroupIds(List<String> groupIds) {
        List<DoorGroupDetail> list = lambdaQuery().in(DoorGroupDetail::getGroupId, groupIds).list();
        return list;
    }

    @Override
    public List<String> listDevice(String groupId) {
        return lambdaQuery()
                .select(DoorGroupDetail::getName)
                .eq(DoorGroupDetail::getGroupId, groupId)
                .list()
                .stream()
                .map(DoorGroupDetail::getName)
                .collect(Collectors.toList());
    }
}
